<?php
namespace app\api\controller;


use app\common\model\UserModel;
use think\Db;

class Goods extends Common
{

    public function getGoodsList(){
        $this->checkSign();
        $field = 'id,goods_title,goods_images,integral';
        $searchKeyWords = input('param.searchKeyWords','');
        $list = Db::name('goods')->whereLike("goods_title","%{$searchKeyWords}%")
            ->where('is_delete',0)
            ->where('is_shop',1)
            ->order('id','desc')
            ->field($field)->select();
        return package(0,'success',$list);
    }
    public function getGoodsInfo(){
        $this->checkSign();
        $id  = input('param.id/d');
        $info = $list = Db::name('goods') ->where('is_delete',0)->where('is_shop',1)->where('id',$id)->find();
        return package(0,'success',$info);
    }
    //兑换
    public function exchange(){
        $data = $this->checkSign();
        $id = intval($data['id']??0);
        $address_id = intval($data['address_id']??0);
        $goodsInfo = Db::name('goods')
            ->where(['id'=>$id,'is_shop'=>1,'is_delete'=>0])
            ->field('id,stock,goods_title,integral,sales,goods_images')
            ->find();
        if(!$goodsInfo){
            return package(1006,'商品不存在');
        }
        if($goodsInfo['stock']<1){
            return package(1006,'库存不足');
        }
        $Address = Db::name('users_address')->where('id',$address_id)->where('user_id',$data['user_id'])->find();
        if(!$Address){
            return package(1006,'地址未找到');
        }

        $user_integral = Db::name('users')->where('id',$data['user_id'])->value('integral');
        if($goodsInfo['integral']>$user_integral){
            return package(1006,'当前积分为'.$user_integral.',积分不足以兑换此商品。请多回收吧！');
        }
        $orderInfo = [
            'user_id'=>$data['user_id'],
            'headimgurl'=>$data['headimgurl'],
            'order_number'=>date("YmdHis").createCode(5),
            'true_name'=>$Address['true_name'],
            'address'=>$Address['address'],
            'floor_address'=>$Address['floor_address'],
            'address_name'=>$Address['address_name'],
            'phone'=>$Address['phone'],
            'latitude'=>$Address['latitude'],
            'longitude'=>$Address['longitude'],
            'status'=>0,
            'create_time'=>date("Y-m-d H:i:s"),
            'goods_id'=>$goodsInfo['id'],
            'goods_title'=>$goodsInfo['goods_title'],
            'goods_images'=>$goodsInfo['goods_images'],
            'integral'=>$goodsInfo['integral']
        ];
        $jifenArr = [
            'user_id'=>$data['user_id'],
            'type'=>2,
            'integral'=>$goodsInfo['integral'],
            'before_integral'=>$user_integral,
            'subject'=>2,
            'after_integral'=>$user_integral-$goodsInfo['integral'],
            'create_time'=>date("Y-m-d H:i:s"),
            'remarks'=>"积分兑换商品"
        ];

        Db::startTrans();
        $result1 = Db::name('exchange_order')->insertGetId($orderInfo);//库存在存储过程里面
        $result2 = Db::name('users_integral')->insert(array_merge($jifenArr,['other_id'=>$result1]));
        if($result1 && $result2){
            Db::commit();
            return package(0,'兑换成功');
        }else{
            Db::rollback();
            return package(1006,'兑换失败');
        }
    }
    public function exchangeOrderRecord(){
        $data = $this->checkSign();
        $status = intval($data['status']??5);
        $where = [['user_id','=',$data['user_id']]];
        if($status !=5){
            $where[] = ['status','=',$status];
        }
        $list  = Db::name('exchange_order')->where($where)
            ->field('id,create_time,phone,true_name,goods_title,goods_images,goods_id,integral,status')
            ->order('id','desc')
            ->select();
        return package(0,'success',[
            'list'=>$list,
            'integral'=>Db::name('users')->where('id',$data['user_id'])->value('integral')
        ]);
    }
    public function shouhuo(){
        $data = $this->checkSign();
        $id = intval($data['id']??0);
        $result  = Db::name('exchange_order')
            ->where('user_id',$data['user_id'])
            ->where('id',$id)
            ->update(['success_time'=>date("Y-m-d H:i:s"),'status'=>2]);
        return checkRes($result,'收货');
    }
}